import models.MyConfig;
import models.ReturnJson;
import models.User;
import tools.ReadProperties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Objects;

/**
 * Created by yangxianming on 2018/1/16.
 * 设备数据上传测试
 */
@WebServlet(name = "home", urlPatterns = {"/home/*"}, loadOnStartup = 2)
public class Home extends HttpServlet {
    MyConfig myConfig = new MyConfig();
    public void init(ServletConfig config) throws ServletException {
        ReadProperties readProperties = new ReadProperties(config.getServletContext().getRealPath("/"));
        myConfig.setPropertiesConfig(readProperties);
        myConfig.setSplitMark();
        myConfig.setUser(new User());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //start
        long start = new Date().getTime();

        response.setHeader("Content-type", "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String returnInfo = "";

        //首字母大写
//        char[] cs = request.getPathInfo().substring(1).toCharArray();
//        cs[0] -= 32;

        Class<?> action;
        try {
            ReturnJson returnJson = new ReturnJson();
            HttpSession sh = request.getSession();
            Cookie[] cookies = request.getCookies();
            if (Objects.equals(cookies, null)){
                //cookie空也返回登陆
                returnJson.setApiStatus(0);
                returnJson.setResult("../../");
            }else {
                for (Cookie cookie: cookies){
                    if (Objects.equals(cookie.getName(), "JSESSIONID")){
                        String c = cookie.getValue();
                        if (Objects.equals(sh.getAttribute(c), null)){
                            //没有登陆请求的跳去登陆界面
                            returnJson.setApiStatus(0);
                            returnJson.setResult("../../");
                        }else {
                            User user = (User)sh.getAttribute(c);
                            returnJson.setResult(user.getUsername());
                        }
                        break;
                    }
                }
            }
            //正常登陆返回用户民
            returnInfo = returnJson.toString();
        }catch (Exception e){
            e.printStackTrace();
        }

        //end
        long end = new Date().getTime();
        System.out.println(request.getRequestURI() + "; run time: " + (end-start) + "ms");

        out.print(returnInfo);
    }

    @Override
    public void destroy()
    {
        // 什么也不做
    }
}
